#include "camera.h"
#include"math.h"
#include <QDebug>

QMatrix4x4 Camera::getViewMat()
{
    QMatrix4x4 view;
    view.lookAt(cameraPos, cameraTarget, cameraUp);
    cameraDirection = cameraTarget - cameraPos;
    return view;
}

void Camera::DealMouseMovement(GLfloat Xoffset, GLfloat Yoffset)
{
    if(moveCamera)
    {
        GLfloat sensitivity = 0.001f;
        cameraPos += QVector3D(sensitivity * Xoffset,-sensitivity * Yoffset, 0.0f);
        cameraTarget += QVector3D(sensitivity * Xoffset,-sensitivity * Yoffset, 0.0f);
    }
}

void Camera::DealMouseScroll(GLfloat Yoffset)
{
    GLfloat moveSensitivity = 0.1f;
    GLfloat max_z = -5.0f,min_z = -50.0f;
    GLfloat depth = cameraPos.z();
    if(depth <= max_z && depth >= min_z){
        cameraPos += QVector3D(0.0f,0.0f,Yoffset * moveSensitivity);
    }
    else if(depth > max_z){
        cameraPos = QVector3D(0.0f,0.0f,max_z);
    }
    else if(depth < min_z){
        cameraPos = QVector3D(0.0f,0.0f,min_z);
    }
}

void Camera::DealKeyboardInputs()
{
    if (keys[Qt::Key_W])
        qDebug()<<"Key_W";
}

void Camera::UpdateCameraLookatMatVectors()
{
    QVector3D Direction(cos(this->yaw) * cos(this->pitch), sin(this->pitch), sin(this->yaw) * cos(this->pitch));
    this->cameraDirection = Direction.normalized();
    this->cameraRight = QVector3D::crossProduct(this->cameraDirection, QVector3D(0.0f, 1.0f, 0.0f)).normalized();
    this->cameraUp = QVector3D::crossProduct(this->cameraRight, this->cameraDirection).normalized();
}

void Camera::DealMouseRightPress(){}
